#!/usr/bin/env python
import wx
import wx.lib.buttons as buttons

data={'common_cold':{'runny_nose':'yes','nasal_blockage':'yes','sneezing':'yes'},'allergic_rhinitis':{'sneezing':'yes','nasal_blockage':'yes'}}

diseaselist=data.keys()


print diseaselist

class RefactorExample(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'DEVEL TOOL For Knowledge Engineer - Add/Edit Rules',
                size=(800, 800))
        self.panel = wx.Panel(self, -1)
        self.panel.SetBackgroundColour("White")
        vbox = wx.BoxSizer(wx.VERTICAL)
        self.panel.SetSizer(vbox)
        self.paneltop=wx.Panel(self.panel,-1,size=(800,200))
        self.paneltop.SetBackgroundColour("lightblue")
        vbox.Add(self.paneltop)                
        self.panelmain=wx.Panel(self.panel,-1)
        self.panelmain.SetBackgroundColour("White")
        vbox.Add(self.panelmain,1,wx.EXPAND | wx.RIGHT)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        self.panelmain.SetSizer(hbox1)
        
        b1=buttons.GenButton(self.paneltop,-1,'Enter Simple Rules',pos=(10,10),size=(210,50))
        b1.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.BOLD, False))
        b1.SetBackgroundColour("maroon")
        b1.SetForegroundColour("white")
        b1.SetToolTipString("Simple Rules in the format: disease name -> symptom name with CF.")
        self.Bind(wx.EVT_BUTTON,self.OnSimple,b1) 

        b2=buttons.GenButton(self.paneltop,-1,'Enter Algorithm Rules',pos=(240,10),size=(230,50))
        b2.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.BOLD, False))
        b2.SetBackgroundColour("#3373ae")
        b2.SetForegroundColour("white")
        b2.SetToolTipString("Forward chaining rules : Algorithm wise x-->y--z")
        self.Bind(wx.EVT_BUTTON,self.OnAlgorithm,b2) 

        b3=buttons.GenButton(self.paneltop,-1,'Enter Treatment Rules',pos=(500,10),size=(230,50))
        b3.SetFont(wx.Font(9, wx.ROMAN, wx.NORMAL, wx.BOLD, False))
        b3.SetBackgroundColour("#3373ae")
        b3.SetForegroundColour("white")
        b3.SetToolTipString("Treatment rules  : Algorithm wise x-->y--z")
        self.Bind(wx.EVT_BUTTON,self.OnTreatment,b3) 


#----------- End of buttons. 

    #       self.paneltop.Show(True)


#------------ Simple Rules Form

        self.scroll1=wx.ScrolledWindow(self.panelmain,-1,pos=wx.DefaultPosition, size=(800,800), style=wx.HSCROLL | wx.VSCROLL)
        self.scroll1.SetScrollbars(1,1,1300,1400)
        hbox1.Add(self.scroll1, 1,flag=wx.EXPAND)
        self.scroll1.SetBackgroundColour("white")
        wx.StaticText(self.scroll1,-1,"Add/Edit Simple Rule(s) Form:", pos=(20,10)).SetFont(wx.Font(12, wx.DECORATIVE,wx.ITALIC, wx.NORMAL))

        sampleList = ['Cardiovascular', 'Pulmonary', 'Nervous System', 'GastroIntestinal', 'Genito Urinary', 'Musculoskeletal','Psychiatry', 'ENT', 'Opthalmology','Rheumatology','Genetics','Paediatrics']
        wx.StaticText(self.scroll1, -1, "Select the organ system:", (80, 40))
        self.sysname=wx.Choice(self.scroll1, -1, (255, 35), choices=sampleList)
        self.sysname.SetStringSelection("Cardiovascular")

        wx.StaticText(self.scroll1, -1, "Please enter the disease name\n(eg.allergic rhinitis)  : ", (80, 80))

        self.dz_name=wx.TextCtrl(self.scroll1, -1, "",size=(255, -1),pos=(310,80))
        dz_checkbutton=wx.Button(self.scroll1,-1,"Check if disease already exists",pos=(310,110),size=(255,25))
        disease="Allergic Rhinitis"
        h1=wx.StaticText(self.scroll1, -1, disease+":" , (80, 150))
        h1.SetFont(wx.Font(13,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))         
        h1.SetForegroundColour("darkblue")


#------Symptoms Form

        global y
        y=135
        self.panel1=wx.Panel(self.scroll1,-1,pos=(80,180),size=(800,y))
        self.panel1.SetBackgroundColour("#ECECEC")

        self.sym_box=wx.StaticBox(self.panel1,-1,"Symptoms:",size=(800,y))

        self.GenSymAddBox()
        self.GenSymptoms()
        global z
        z=115
        self.panel2=wx.Panel(self.scroll1,-1,pos=(80,220+y),size=(800,z))
        self.sign_box=wx.StaticBox(self.panel2,-1,"Signs:",size=(800,z))        

        self.GenSignAddBox()
        self.GenSigns()

        t=y+z
        global ez
        ez=115
        self.panel3=wx.Panel(self.scroll1,-1,pos=(80,250+t),size=(800,ez))
        self.inv_box=wx.StaticBox(self.panel3,-1,"Investigations:",size=(800,ez))      
        self.panel3.SetBackgroundColour("#ECECEC")          

        self.GenInvAddBox()
        self.GenInv()        
        
    def OnSimple(self,event):
        pass
    def OnAlgorithm(self,event):
        pass
    def OnTreatment(self,event):
        pass              
    def GenSymAddBox(self):
        self.syntax=wx.StaticText(self.panel1,-1,"Please enter the answer prefixed by symptom name eg. cough_gradual for TB .Use hyphens '-' for words\nwithin question eg. allergy-cold_yes. Enter their certainty ranging from -1 to 0 to 1", pos=(15,25))
        self.syntax.SetFont(wx.Font(9,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))        
        self.syntax.SetForegroundColour("Maroon")
        self.addsym=wx.StaticText(self.panel1,-1,"Name:",pos=(15,70))
        self.addsym.SetFont(wx.Font(10,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))
        self.addsym=wx.TextCtrl(self.panel1, -1, "",size=(225, -1),pos=(70,65))
        self.addsymcf=wx.StaticText(self.panel1,-1,"Certainty Factor:",pos=(305,70))        
        self.addsymcf.SetFont(wx.Font(10,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))
        self.addsymcf=wx.TextCtrl(self.panel1, -1, "0.6",size=(45, -1),pos=(420,65))
       

        self.add_btn=buttons.GenButton(self.panel1,-1,'Add!',pos=(490,65),size=(50,25))
        self.add_btn.SetFont(wx.Font(10, wx.ROMAN, wx.NORMAL, wx.ITALIC, False))
        self.add_btn.SetBackgroundColour("#3373ae")
        self.add_btn.SetForegroundColour("white")
        self.Bind(wx.EVT_BUTTON,self.OnSymAdd,self.add_btn)         

        wx.StaticText(self.panel1,-1,"___________________________________________________________",pos=(50,95))    



    def GenSymptoms(self):
        self.sym_dict={'runny_nose_yes':'0.5','sneezing_yes':'0.6','sorethroat_yes':'0.4'}
        ls = self.sym_dict.iteritems()
        ls=data.values()[0]
        print ls
        global ranger
        ranger=1
        for name,cf in self.sym_dict.iteritems():
            global y
            self.sym_name=wx.StaticText(self.panel1,ranger+0,"Name:",pos=(15,y+5))
            self.sym_name.SetFont(wx.Font(10,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))
            self.symname =wx.TextCtrl(self.panel1, ranger+1, name,size=(225, -1),pos=(70,y))
            self.symname.Enable(False)
            self.cf_name=wx.StaticText(self.panel1,ranger+2,"Certainty Factor:",pos=(305,y+5))        
            self.cf_name.SetFont(wx.Font(10,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))
            self.cf_input=wx.TextCtrl(self.panel1, ranger+3, cf,size=(45, -1),pos=(420,y))
            self.cf_input.Enable(False)
                       
            self.edit_btn=buttons.GenButton(self.panel1,ranger+4,'Edit!',pos=(490,y),size=(50,25))
            self.edit_btn.SetFont(wx.Font(10, wx.ROMAN, wx.NORMAL, wx.ITALIC, False))
            self.edit_btn.SetBackgroundColour("#3373ae")
            self.edit_btn.SetForegroundColour("white") 
            self.Bind(wx.EVT_BUTTON,self.OnSymEdit,self.edit_btn)  
            
            self.del_btn=buttons.GenButton(self.panel1,ranger+5,'Delete!',pos=(550,y),size=(70,25))
            self.del_btn.SetFont(wx.Font(10, wx.ROMAN, wx.NORMAL, wx.ITALIC, False))
            self.del_btn.SetBackgroundColour("#3373ae")
            self.del_btn.SetForegroundColour("white")  
            self.Bind(wx.EVT_BUTTON,self.OnSymDelete,self.del_btn)                        
            ranger=ranger+6
                      

            y=y+35
            self.panel1.SetSize((650,y))
            self.sym_box.SetSize((650,y))   
            print y     

    def GenSignAddBox(self):
        self.syntax=wx.StaticText(self.panel2,-1,"Please enter the answer prefixed by symptom name eg. cough_gradual for TB .Use hyphens '-' for words\nwithin question eg. allergy-cold_yes. Enter their certainty ranging from -1 to 0 to 1", pos=(15,25))
        self.syntax.SetFont(wx.Font(9,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))        
        self.syntax.SetForegroundColour("Maroon")
        self.addsign=wx.StaticText(self.panel2,-1,"Name:",pos=(15,70))
        self.addsign.SetFont(wx.Font(10,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))
        self.addsign=wx.TextCtrl(self.panel2, -1, "",size=(225, -1),pos=(70,65))
        self.addsigncf=wx.StaticText(self.panel2,-1,"Certainty Factor:",pos=(305,70))        
        self.addsigncf.SetFont(wx.Font(10,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))
        self.addsigncf=wx.TextCtrl(self.panel2, -1, "0.6",size=(45, -1),pos=(420,65))
       

        self.add_btn=buttons.GenButton(self.panel2,-1,'Add!',pos=(490,65),size=(50,25))
        self.add_btn.SetFont(wx.Font(10, wx.ROMAN, wx.NORMAL, wx.ITALIC, False))
        self.add_btn.SetBackgroundColour("#3373ae")
        self.add_btn.SetForegroundColour("white")
        self.Bind(wx.EVT_BUTTON,self.OnSignAdd,self.add_btn)         

        wx.StaticText(self.panel1,-1,"___________________________________________________________",pos=(50,95))    



    def GenSigns(self):
        self.sym_dict={'runny_nose_yes':'0.5','sneezing_yes':'0.6','sorethroat_yes':'0.4'}
        ls = self.sym_dict.iteritems()
        ls=data.values()[0]
        print ls
        global ranger
        for name,cf in self.sym_dict.iteritems():
            global z
            self.sym_name=wx.StaticText(self.panel2,ranger+0,"Name:",pos=(15,z+5))
            self.sym_name.SetFont(wx.Font(10,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))
            self.symname =wx.TextCtrl(self.panel2, ranger+1, name,size=(225, -1),pos=(70,z))
            self.symname.Enable(False)
            self.cf_name=wx.StaticText(self.panel2,ranger+2,"Certainty Factor:",pos=(305,z+5))        
            self.cf_name.SetFont(wx.Font(10,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))
            self.cf_input=wx.TextCtrl(self.panel2, ranger+3, cf,size=(45, -1),pos=(420,z))
            self.cf_input.Enable(False)
                       
            self.edit_btn=buttons.GenButton(self.panel2,ranger+4,'Edit!',pos=(490,z),size=(50,25))
            self.edit_btn.SetFont(wx.Font(10, wx.ROMAN, wx.NORMAL, wx.ITALIC, False))
            self.edit_btn.SetBackgroundColour("#3373ae")
            self.edit_btn.SetForegroundColour("white") 
            self.Bind(wx.EVT_BUTTON,self.OnSignEdit,self.edit_btn)  
            
            self.del_btn=buttons.GenButton(self.panel2,ranger+5,'Delete!',pos=(550,z),size=(70,25))
            self.del_btn.SetFont(wx.Font(10, wx.ROMAN, wx.NORMAL, wx.ITALIC, False))
            self.del_btn.SetBackgroundColour("#3373ae")
            self.del_btn.SetForegroundColour("white")  
            self.Bind(wx.EVT_BUTTON,self.OnSignDelete,self.del_btn)                        
            ranger=ranger+6


            z=z+35
            self.panel2.SetSize((650,z))
            self.sign_box.SetSize((650,z))        



    def GenInvAddBox(self):
        self.syntax=wx.StaticText(self.panel3,-1,"Please enter the answer prefixed by symptom name eg. cough_gradual for TB .Use hyphens '-' for words\nwithin question eg. allergy-cold_yes. Enter their certainty ranging from -1 to 0 to 1", pos=(15,25))
        self.syntax.SetFont(wx.Font(9,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))        
        self.syntax.SetForegroundColour("Maroon")
        self.addinv=wx.StaticText(self.panel3,-1,"Name:",pos=(15,70))
        self.addinv.SetFont(wx.Font(10,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))
        self.addinv=wx.TextCtrl(self.panel3, -1, "",size=(225, -1),pos=(70,65))
        self.addinvcf=wx.StaticText(self.panel3,-1,"Certainty Factor:",pos=(305,70))        
        self.addinvcf.SetFont(wx.Font(10,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))
        self.addinvcf=wx.TextCtrl(self.panel3, -1, "0.6",size=(45, -1),pos=(420,65))
       

        self.add_btn=buttons.GenButton(self.panel3,-1,'Add!',pos=(490,65),size=(50,25))
        self.add_btn.SetFont(wx.Font(10, wx.ROMAN, wx.NORMAL, wx.ITALIC, False))
        self.add_btn.SetBackgroundColour("#3373ae")
        self.add_btn.SetForegroundColour("white")
        self.Bind(wx.EVT_BUTTON,self.OnInvAdd,self.add_btn)         

        wx.StaticText(self.panel3,-1,"___________________________________________________________",pos=(50,95))    



    def GenInv(self):
        self.sym_dict={'runny_nose_yes':'0.5','sneezing_yes':'0.6','sorethroat_yes':'0.4'}
        ls = self.sym_dict.iteritems()
        ls=data.values()[0]
        print ls
        global ranger
        for name,cf in self.sym_dict.iteritems():
            global ez
            self.sym_name=wx.StaticText(self.panel3,ranger+0,"Name:",pos=(15,ez+5))
            self.sym_name.SetFont(wx.Font(10,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))
            self.symname =wx.TextCtrl(self.panel3, ranger+1, name,size=(225, -1),pos=(70,ez))
            self.symname.Enable(False)
            self.cf_name=wx.StaticText(self.panel3,ranger+2,"Certainty Factor:",pos=(305,ez+5))        
            self.cf_name.SetFont(wx.Font(10,wx.DECORATIVE,wx.ITALIC,wx.NORMAL))
            self.cf_input=wx.TextCtrl(self.panel3, ranger+3, cf,size=(45, -1),pos=(420,ez))
            self.cf_input.Enable(False)
                       
            self.edit_btn=buttons.GenButton(self.panel3,ranger+4,'Edit!',pos=(490,ez),size=(50,25))
            self.edit_btn.SetFont(wx.Font(10, wx.ROMAN, wx.NORMAL, wx.ITALIC, False))
            self.edit_btn.SetBackgroundColour("#3373ae")
            self.edit_btn.SetForegroundColour("white") 
            self.Bind(wx.EVT_BUTTON,self.OnInvEdit,self.edit_btn)  
            
            self.del_btn=buttons.GenButton(self.panel3,ranger+5,'Delete!',pos=(550,ez),size=(70,25))
            self.del_btn.SetFont(wx.Font(10, wx.ROMAN, wx.NORMAL, wx.ITALIC, False))
            self.del_btn.SetBackgroundColour("#3373ae")
            self.del_btn.SetForegroundColour("white")  
            self.Bind(wx.EVT_BUTTON,self.OnInvDelete,self.del_btn)                        
            ranger=ranger+6


            ez=ez+35
            self.panel3.SetSize((650,ez))
            self.inv_box.SetSize((650,ez))        



    def OnSymEdit(self,event):
        wid=event.GetId()

        but=self.panel1.FindWindowById(wid)
        but.SetLabel("Save!")
        self.Bind(wx.EVT_BUTTON,self.SaveSymEdit,but)
        symid=wid-3
        sym=self.panel1.FindWindowById(symid)
        sym.Enable(True)
        cfid=wid-1
        cf=self.panel1.FindWindowById(cfid)
        cf.Enable(True)
        delid=wid+1
        delbtn=self.panel1.FindWindowById(delid)        
        delbtn.Enable(False)
        
        
    def SaveSymEdit(self,event):
        wid=event.GetId()   
        but=self.panel1.FindWindowById(wid)
        self.Bind(wx.EVT_BUTTON,self.OnSymEdit,but)        
        but.SetLabel("Edit!")         
        symid=wid-3
        sym=self.panel1.FindWindowById(symid)
        sym.Enable(False)
        cfid=wid-1
        cf=self.panel1.FindWindowById(cfid)
        cf.Enable(False)    
        delid=wid+1
        delbtn=self.panel1.FindWindowById(delid)        
        delbtn.Enable(True)

    #    print sym.GetValue(),cf.GetValue()
    #    print "saved!"
        
      #  print self.sysname.GetStringSelection()



    def OnSymDelete(self,event):
        wid=event.GetId()
        but=self.panel1.FindWindowById(wid)
        symstid=wid-5
        symst=self.panel1.FindWindowById(symstid)
        symst.Destroy()        
        symid=wid-4
        sym=self.panel1.FindWindowById(symid)
        value= sym.GetValue()
        
        sym.Destroy()
        cfstid=wid-3
        cfst=self.panel1.FindWindowById(cfstid)
        cfst.Destroy()        
        cfid=wid-2
        cf=self.panel1.FindWindowById(cfid)
        cf.Destroy()
        editid=wid-1
        editbtn=self.panel1.FindWindowById(editid)
        editbtn.Destroy()
        but.Destroy()

        global y

    def OnSymAdd(self,event):
        print self.addsym.GetValue(), self.addsymcf.GetValue()
        self.sym_dict[self.addsym.GetValue()]=self.addsymcf.GetValue()
        self.addsym.SetValue("")
        self.addsymcf.SetValue("")
     #   print self.sym_dict

        # Refresh Panel.
        pass


        



    def OnSignEdit(self,event):
        wid=event.GetId()

        but=self.panel2.FindWindowById(wid)
        but.SetLabel("Save!")
        self.Bind(wx.EVT_BUTTON,self.SaveSignEdit,but)
        symid=wid-3
        sym=self.panel2.FindWindowById(symid)
        sym.Enable(True)
        cfid=wid-1
        cf=self.panel2.FindWindowById(cfid)
        cf.Enable(True)
        delid=wid+1
        delbtn=self.panel2.FindWindowById(delid)        
        delbtn.Enable(False)
        
        
    def SaveSignEdit(self,event):
        wid=event.GetId()   
        but=self.panel2.FindWindowById(wid)
        self.Bind(wx.EVT_BUTTON,self.OnSignEdit,but)        
        but.SetLabel("Edit!")         
        symid=wid-3
        sym=self.panel2.FindWindowById(symid)
        sym.Enable(False)
        cfid=wid-1
        cf=self.panel2.FindWindowById(cfid)
        cf.Enable(False)    
        delid=wid+1
        delbtn=self.panel2.FindWindowById(delid)        
        delbtn.Enable(True)

    #    print sym.GetValue(),cf.GetValue()
    #    print "saved!"
        
      #  print self.sysname.GetStringSelection()



    def OnSignDelete(self,event):
        wid=event.GetId()
        but=self.panel2.FindWindowById(wid)
        symstid=wid-5
        symst=self.panel2.FindWindowById(symstid)
        symst.Destroy()        
        symid=wid-4
        sym=self.panel2.FindWindowById(symid)
        value= sym.GetValue()
        
        sym.Destroy()
        cfstid=wid-3
        cfst=self.panel2.FindWindowById(cfstid)
        cfst.Destroy()        
        cfid=wid-2
        cf=self.panel2.FindWindowById(cfid)
        cf.Destroy()
        editid=wid-1
        editbtn=self.panel2.FindWindowById(editid)
        editbtn.Destroy()
        but.Destroy()

        global y

    def OnSignAdd(self,event):
        print self.addsign.GetValue(), self.addsigncf.GetValue()
        self.sym_dict[self.addsign.GetValue()]=self.addsigncf.GetValue()
        self.addsign.SetValue("")
        self.addsigncf.SetValue("")
     #   print self.sym_dict

        # Refresh Panel.
        pass





    def OnInvEdit(self,event):
        wid=event.GetId()

        but=self.panel3.FindWindowById(wid)
        but.SetLabel("Save!")
        self.Bind(wx.EVT_BUTTON,self.SaveInvEdit,but)
        symid=wid-3
        sym=self.panel3.FindWindowById(symid)
        sym.Enable(True)
        cfid=wid-1
        cf=self.panel3.FindWindowById(cfid)
        cf.Enable(True)
        delid=wid+1
        delbtn=self.panel3.FindWindowById(delid)        
        delbtn.Enable(False)
        
        
    def SaveInvEdit(self,event):
        wid=event.GetId()   
        but=self.panel3.FindWindowById(wid)
        self.Bind(wx.EVT_BUTTON,self.OnInvEdit,but)        
        but.SetLabel("Edit!")         
        symid=wid-3
        sym=self.panel3.FindWindowById(symid)
        sym.Enable(False)
        cfid=wid-1
        cf=self.panel3.FindWindowById(cfid)
        cf.Enable(False)    
        delid=wid+1
        delbtn=self.panel3.FindWindowById(delid)        
        delbtn.Enable(True)

    #    print sym.GetValue(),cf.GetValue()
    #    print "saved!"
        
      #  print self.sysname.GetStringSelection()



    def OnInvDelete(self,event):
        wid=event.GetId()
        but=self.panel3.FindWindowById(wid)
        symstid=wid-5
        symst=self.panel3.FindWindowById(symstid)
        symst.Destroy()        
        symid=wid-4
        sym=self.panel3.FindWindowById(symid)
        value= sym.GetValue()
        
        sym.Destroy()
        cfstid=wid-3
        cfst=self.panel3.FindWindowById(cfstid)
        cfst.Destroy()        
        cfid=wid-2
        cf=self.panel3.FindWindowById(cfid)
        cf.Destroy()
        editid=wid-1
        editbtn=self.panel3.FindWindowById(editid)
        editbtn.Destroy()
        but.Destroy()

        global y

    def OnInvAdd(self,event):
        print self.addinv.GetValue(), self.addinvcf.GetValue()
        self.sym_dict[self.addinv.GetValue()]=self.addinvcf.GetValue()
        self.addinv.SetValue("")
        self.addinvcf.SetValue("")
     #   print self.sym_dict

        # Refresh Panel.
        pass

        
              

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = RefactorExample(parent=None, id=-1)
    frame.Show()
    app.MainLoop()
